package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.RedisService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    public UserService userService;

    @Autowired
    public RedisService redisService;

    @GetMapping("/queryById")
    public String queryById(int id) {
        String userStr = redisService.get(id + "");
        if (userStr != null) {
            redisService.set(id + "", userService.queryById(id).toString());
        }
        return userStr;
    }
    @PostMapping("/save")
    public User saveUser(String name,int age){
        return userService.save(new User(name,age));

    }

}